﻿$(function () {
    $("#btnSubmitHelp").live("click", btnSubmitHelpClick);
    $(".EnabledClick").live("click", viewSosDetail);
    $(".btnCommitSos").live("click", btnCommitSosClick);
    $("#btnCloseCommitSos").live("click", function () { $("#commentSos").slideUp() });
    $("#btnCommit").live("click", btnCommitClick);
    
    function btnSubmitHelpClick() {
        var radioValue = $("input[name='Type']:checked").val();
        var selectedValue = $("#Category").val();
        var content = $("#Content").val();
        if (radioValue == undefined) {
            alert("请勾选事物类型！");
            return;
        }
        if (selectedValue == "-1") {
            alert("请选择事物分类！");
            $("#Category").focus();
            return;
        }
        if ($.trim(content) == "") {
            alert("正文不得为空！");
            $("#Content").focus();
            return;
        }
        $("#HelpRequestForm").submit();
    }

    var info = $("#hiddenInfo").val();
    if (info == "true") {
        //location.reload();
        alert("提交成功");
    } else
        if ($.trim(info) != "") {
            alert(info);
        }

    var Page_Size = 10;
    GetflowLogPage({ page: 1, pageSize: Page_Size });

    function GetflowLogPage(f) {
        //分页控件   
        var count = GetDataCount(f);
        $("#pp").pagination(count, {
            link_to: "javascript:void(0)",
            num_edge_entries: 2, //边缘页数
            num_display_entries: 7, //主体页数
            callback: onSelectPageFun,
            items_per_page: Page_Size, //每页显示项
            prev_text: "&laquo;",
            next_text: "&raquo;"
        });

        //响应事件
        function onSelectPageFun(page) {
            f.page = page + 1;
            var url = "../SOS/GetSOSList";
            $.ajax({
                url: url,
                data: f,
                dataType: "json",
                beforeSend: function () {

                },
                type: "post",
                success: function (result) {
                    renderFun(result);//响应事件
                },
                async: true,
                complete: function () {
                }
            });
        }
        //END
    };

    Handlebars.registerHelper('sosHideInfo', function (sos) {
        var res = "";
        for (var p in sos) {
            if (p == "EnabledCommit") continue;
            res += "'" + p + "':'" + sos[p] + "',";
        }
        res = "{" + res.substring(0, (res.length - 1)) + "}";
        return res;
    });

    //数据渲染
    function renderFun(result) {
        var $tbody = $("#tbSOS tbody");
        $tbody.empty();
        var list = result;
        var source = $("#sos-template").html();
        var template = Handlebars.compile(source);
        $.each(list, function (key, value) {
            var EnabledCommit = false;
            if (value["Status"] == "完毕" && (value["Comment"]=="未评价")) {
                EnabledCommit = true;
            };
            if (value["Comment"] == "未评价")
            {
                value["Comment"] = "";
            }
            value.EnabledCommit = EnabledCommit;
        });
        var html = template({ sos: list });
        $tbody.append(html);
    }
    function GetDataCount(f) {
        var count = 0;
        $.ajax({
            url: "../SOS/GetSOSCount",
            data: f,
            success: function (res) {
                count = res;
            },
            async: false
        });
        return count;
    };

    function viewSosDetail() {
        var sosId = $(this).attr("data-sosid");
        $.getJSON("../SOS/GetSOSById", { sosId: sosId ,now:new Date()}, function (result) {
            var source = $("#sosInfo-template").html();
            var template = Handlebars.compile(source);
            var html = template(result);
            myDialog = art.dialog({
                content: html,
                id: 'sosInfoDialog',
            }).lock();
        });
    };

    function btnCommitSosClick(e) {
        var sosId = $(this).attr("data-sosId");
        $("#commentSos").slideDown();
        $("#handlerSosId").val(sosId);
        // $(":input[name='commentSosRadio']").first().focus();
    };

    function btnCommitClick()
    {
        var sosId = $("#handlerSosId").val();
        var commentValue = $(":input[name='commentSosRadio']:checked").val();
        if (!sosId) {
            alert("援助id丢失");
        }
        else if (!commentValue) {
            alert("请选择评价");
            $(":input[name='commentSosRadio']").first().focus();
        } else {
            $.getJSON("../SOS/CommentSos", { id: sosId, comment: commentValue, now: new Date() }, function (result) {
                if (result.Result.IsSuccess) {
                    alert("点评成功");
                    $("#commentSos").slideUp();
                    $("#handlerSosId").val("");
                    GetflowLogPage({ page: 1, pageSize: Page_Size });
                }
                else {
                    alert(result.Result.ErrMessage)
                }
            });
        }
    }
});